package com.app.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeHelper {
    private static String CurrentTime;
    private static String CurrentDate;

    public static String getCurrentYear() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(NowDate);
    }

    public static String getCurrentMonth() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("MM");
        return formatter.format(NowDate);
    }

    public static String getCurrentDay() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("dd");
        return formatter.format(NowDate);
    }

    public static String getCurrentTime() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return TimeHelper.CurrentTime = formatter.format(NowDate);
    }

    public static Date getCurrentDateTime() {
        final Date NowDate = new Date();
        return NowDate;
    }

    public static String getCurrentCompactTime() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return TimeHelper.CurrentTime = formatter.format(NowDate);
    }

    public static String getCurrentCompactTime1() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return TimeHelper.CurrentTime = formatter.format(NowDate);
    }

    public static String getYesterdayCompactTime() {
        final Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        System.out.print(cal.getTime());
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return TimeHelper.CurrentTime = formatter.format(cal.getTime());
    }

    public static String getYesterdayCompactTimeForFileName() {
        final Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        System.out.print(cal.getTime());
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return TimeHelper.CurrentTime = formatter.format(cal.getTime());
    }

    public static String getCurrentDate() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return TimeHelper.CurrentDate = formatter.format(NowDate);
    }

    public static String getCurrentDate1() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        return TimeHelper.CurrentDate = formatter.format(NowDate);
    }

    public static String getCurrentFirstDate() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
        return TimeHelper.CurrentDate = formatter.format(NowDate);
    }

    public static String getCurrentLastDate() {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = null;
        try {
            final Date date = formatter.parse(getCurrentFirstDate());
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(2, 1);
            calendar.add(6, -1);
            return formatDate(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatDate(final Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    public static String formatDateByFormat(final Date date, final String format) {
        String result = "";
        if (date != null) {
            try {
                final SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String addDay(final String currentdate, final int add_day) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            final int year = Integer.parseInt(currentdate.substring(0, 4));
            final int month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdate.substring(8, 10));
            gc = new GregorianCalendar(year, month, day);
            gc.add(5, add_day);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String addMonth(final String currentdate, final int add_month) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            final int year = Integer.parseInt(currentdate.substring(0, 4));
            final int month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdate.substring(8, 10));
            gc = new GregorianCalendar(year, month, day);
            gc.add(2, add_month);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int monthDiff(final String beforeTime, final String endTime) {
        if (beforeTime == null || endTime == null) {
            return 0;
        }
        try {
            final int beforeYear = Integer.parseInt(beforeTime.substring(0, 4));
            final int endYear = Integer.parseInt(endTime.substring(0, 4));
            final int beforeMonth = Integer.parseInt(beforeTime.substring(5, 7)) - 1;
            final int endMonth = Integer.parseInt(endTime.substring(5, 7)) - 1;
            return (endYear - beforeYear) * 12 + (endMonth - beforeMonth);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String addMinute(final String currentdatetime, final int add_minute) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            final int year = Integer.parseInt(currentdatetime.substring(0, 4));
            final int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdatetime.substring(8, 10));
            final int hour = Integer.parseInt(currentdatetime.substring(11, 13));
            final int minute = Integer.parseInt(currentdatetime.substring(14, 16));
            final int second = Integer.parseInt(currentdatetime.substring(17, 19));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(12, add_minute);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String addSecond(final String currentdatetime, final int add_second) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            final int year = Integer.parseInt(currentdatetime.substring(0, 4));
            final int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdatetime.substring(8, 10));
            final int hour = Integer.parseInt(currentdatetime.substring(11, 13));
            final int minute = Integer.parseInt(currentdatetime.substring(14, 16));
            final int second = Integer.parseInt(currentdatetime.substring(17, 19));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(13, add_second);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String addMinute1(final String currentdatetime, final int add_minute) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            final int year = Integer.parseInt(currentdatetime.substring(0, 4));
            final int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdatetime.substring(8, 10));
            final int hour = Integer.parseInt(currentdatetime.substring(8, 10));
            final int minute = Integer.parseInt(currentdatetime.substring(8, 10));
            final int second = Integer.parseInt(currentdatetime.substring(8, 10));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(12, add_minute);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date parseDate(final String sDate) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            final Date date = bartDateFormat.parse(sDate);
            return date;
        } catch (Exception ex) {
            return null;
        }
    }
    public static String parseDate(final Date sDate) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String format = bartDateFormat.format(sDate);
            return format;
        } catch (Exception ex) {
            return null;
        }
    }
    public static String parseDate1(final Date sDate) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String format = bartDateFormat.format(sDate);
            return format;
        } catch (Exception ex) {
            return null;
        }
    }
    public static Date parseDateTime(final String sDateTime) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            final Date date = bartDateFormat.parse(sDateTime);
            return date;
        } catch (Exception ex) {
            return null;
        }
    }

    public static int getTotalDaysOfMonth(final String strDate) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        final Calendar calendar = new GregorianCalendar();
        Date date = new Date();
        try {
            date = sdf.parse(strDate);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        calendar.setTime(date);
        final int month = calendar.get(2) + 1;
        final int day = calendar.getActualMaximum(5);
        return day;
    }

    public static long getDateSubDay(final String startDate, final String endDate) {
        final Calendar calendar = Calendar.getInstance();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long theday = 0L;
        try {
            calendar.setTime(sdf.parse(startDate));
            final long timethis = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse(endDate));
            final long timeend = calendar.getTimeInMillis();
            theday = (timethis - timeend) / 86400000L;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return theday;
    }

    public static int getCurrentWeek() {
        final Calendar cal = Calendar.getInstance();
        final int week = 7;
        if (cal.get(7) != 1) {
            return cal.get(7) - 1;
        }
        return week;
    }

    public static long getTimestamp(String time, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long s = date.getTime();
        return s ;
    }
    public static long getTimestamp1(String time, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long s = date.getTime();
        return s/1000 ;
    }
    public static String calFullDate(String time, String pattern) {

        SimpleDateFormat sd = new SimpleDateFormat(pattern);
        String format = null;
        try {
            Date parse = sd.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            cal.add(Calendar.SECOND, +5);

            format = sd.format(cal.getTime());


        } catch (ParseException e) {
            e.printStackTrace();
        }

        return format;
    }

    public static Long subTime(String startTime, String endTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime = 0;
        long OTime = 0;
        try {
            NTime = df.parse(startTime).getTime();
            OTime = df.parse(endTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //从对象中拿到时间

        long diff = (OTime - NTime);
        return diff/1000;
    }
    /**
     * 生成随机时间
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static String randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            Date start = format.parse(beginDate);// 构造开始日期
            Date end = format.parse(endDate);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            if (start.getTime() >= end.getTime()) {
                return null;
            }
            long date = random(start.getTime(), end.getTime());
            return timeStamp2Date(String.valueOf(date),"HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static long random(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }
        return rtn;
    }

    public static String timeStamp2Date(String time,String formart) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat(formart);//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String getJzrq(String zlrq,String formart,int day) {
        SimpleDateFormat dff = new SimpleDateFormat(formart);

        Calendar calendars = Calendar.getInstance(); //得到日历
        try {
            calendars.setTime(dff.parse(zlrq));//把当前时间赋给日历
        } catch (ParseException e) {
            e.printStackTrace();
        }

        calendars.add(Calendar.DAY_OF_MONTH, day);  //设置为前一天
        Date dBefore = calendars.getTime();   //得到前一天的时间

        return dff.format(dBefore);

    }
    public static long getTime(String startTime, String endTime)  {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long eTime = 0;
        long sTime =0;
        try {
            eTime = df.parse(endTime).getTime();
            sTime=  df.parse(startTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long diff = (eTime - sTime) / 1000;
        return diff;
    }
    public static String transToUTCDate(String strDate) throws ParseException{
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

        //format将Date型转换为String型，parse将String型转换为Date型
        String UTCDate = sdf.format(date);
        return UTCDate;
    }

    public static void main(final String[] args) throws ParseException {

//        System.out.println(subTime("2020-11-25 00:00:00", "2020-11-25 00:01:00"));

       /* String s = randomDate("07:00:00", "08:00:00");
        System.out.println(s);*/
        String s = transToUTCDate("2023-07-04 22:46:16");
        System.out.println(s);
    /*    long l = calFullDate("2020-11-25", "yyyy-MM-dd");
        System.out.println(l);*/
    }
}
